<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('姓名')" prop="name">
            <el-input v-model="form.name" :placeholder="$t('姓名')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('邮箱')" prop="email">
            <el-input v-model="form.email" :placeholder="$t('邮箱')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('手机')" prop="phone">
            <el-input v-model="form.phone" :placeholder="$t('手机')" show-word-limit maxlength="25" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('职务')" prop="position">
            <el-input v-model="form.position" :placeholder="$t('职务')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('是否在职')">
            <el-radio-group v-model="form.status">
              <el-radio
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{ dict.dictLabel }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="mediation:team" />
    </div>
  </div>
</template>

<script>
import { saveMediationTeamMember, getMediationTeamMember } from '@/api/deal/mediation/mediationTeam/mediationTeamMember'
export default {
  props: {
    teamId: {
      type: String,
      default: ''
    }
  },
  data() {
    const checkEmail = (rule, value, callback) => {
      if (value === '' || value === undefined || value === null) {
        callback(new Error(this.$t('必填信息')))
      } else {
        const reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
        if (!reg.test(value)) {
          callback(new Error(this.$t('请输入有效的邮箱')))
        } else {
          callback()
        }
      }
    }
    return {
      // 是否在职字典
      statusOptions: [],
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        name: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        email: [{ required: true, validator: checkEmail, trigger: 'change' }],
        status: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getDicts('sys_yes_no').then(response => {
      this.statusOptions = response.data
    })
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getMediationTeamMember(data.id).then(response => {
          this.form = response.data
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      } else {
        this.reset()
        if (data && data.teamId) {
          this.form.teamId = data.teamId
        }
        this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        teamId: this.teamId,
        name: undefined,
        email: undefined,
        phone: undefined,
        position: undefined,
        status: 'Y',
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      if (this.$parent.$parent.memberFormOpen !== undefined) {
        this.$parent.$parent.memberFormOpen = false
      } else {
        this.$parent.$parent.reset()
      }
    },
    // 关闭form页面
    closeForm(form) {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      if (this.$parent.$parent.processingDataEcho !== undefined) {
        this.$parent.$parent.processingDataEcho(form, 'mediationTeamMemberForm')
      }
      if (this.$parent.$parent.memberFormOpen !== undefined) {
        this.$parent.$parent.memberFormOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
      if (this.$parent.$parent.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          saveMediationTeamMember(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm(response.data)
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      }).catch(() => {
        this.loading = false
      })
    }
  }
}
</script>
